package com.gage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.gage.commons.result.PageInfo;
import com.gage.commons.result.catalogTree;
import com.gage.mapper.CmsCatalogMapper;
import com.gage.model.CmsCatalog;
import com.gage.service.ICmsCatalogService;
@Transactional(rollbackFor = Exception.class)
@Service(ICmsCatalogService.SERVICE_NAME)
public class CmsCatalogServiceImpl implements ICmsCatalogService{
	private List<Integer> temp = new ArrayList<Integer>();
	@Autowired
	private CmsCatalogMapper cmsCatalogMapper;
	@Override
	public String getCatalogNavigator(int i, Integer id) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public CmsCatalog loadCatalogByCatalogCode(String code) {
		// TODO Auto-generated method stub
		return cmsCatalogMapper.loadCatalog(code);
	}
	@Override
	public String getCatalogTreeByParentIdAndType(String pid,String catalogType) {
		List<catalogTree> lt = new ArrayList<catalogTree>();
		List<CmsCatalog> list = new ArrayList<CmsCatalog>();
		list = cmsCatalogMapper.findCatalogByPidAndType(pid,catalogType);
		for (CmsCatalog o : list)
		{
			catalogTree t = new catalogTree();
			t.setId(o.getCatalogCode());
			t.setText(o.getCatalogName());
			int count = cmsCatalogMapper.getCountChild(o.getCatalogCode(),catalogType);
			if (count > 0)
			{
				t.setState("closed");
			} else
			{
				t.setState("open");
			}
			lt.add(t);
		}

		JSONArray array = JSONArray.fromObject(lt);
		return array.toString();
	}
	@Override
	public PageInfo selectDataGrid(PageInfo pageInfo) {
		 Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
	        page.setOrderByField(pageInfo.getSort());
	        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
	        List<Map<String, Object>> list = cmsCatalogMapper.selectCatalogPage(page, pageInfo.getCondition());
	        pageInfo.setRows(list);
	        pageInfo.setTotal(page.getTotal());
	        return pageInfo;
	}
	@Override
	public List<CmsCatalog> findCatalogByPidAndType(String Pid,String catalogType) {
		// TODO Auto-generated method stub
		return cmsCatalogMapper.findCatalogByPidAndType(Pid,catalogType);
	}
	@Override
	public boolean removeCatalog(List<String> ids) {
		boolean stutas=false;
		if (cmsCatalogMapper.deleteByIds(ids)>0) {
			stutas=true;
		};
		return stutas;
	}

	@Override
	public int updateByCatalogCodeSelective(CmsCatalog cmsCatalog) {
		// TODO Auto-generated method stub
		return cmsCatalogMapper.updateByCatalogCodeSelective(cmsCatalog);
	}
	@Override
	public int insertSelective(CmsCatalog cmsCatalog) {
		// TODO Auto-generated method stub
		return cmsCatalogMapper.insertSelective(cmsCatalog);
	}
	@Override
	public Integer getMaxSequence(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getLevel(String id) {
		try{
			Integer i= this.cmsCatalogMapper.getLevel(id);
			if (i==null){
				return 1;
			}else{
				return i+1;
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
			return 1;
		}
	}

	@Override
	public int updateMoveCatalog(String catalogid, String parentid) {
		try{
			int i=0,olevel=0,plevel=0;
			List<Integer> cids=this.getAllChildOrgaIdList(catalogid);
			if (!"0".equals(parentid)&&cids!=null&&cids.size()>0){
				for(Integer id:cids){
					if (parentid.equals(id.toString())){
						return 0;
					}
				}
			}
			plevel=this.getLevel(catalogid);
			CmsCatalog o=cmsCatalogMapper.loadCatalog(catalogid);
			olevel=o.getCatalogLevel();
			i=plevel-olevel;
			o.setParentId(parentid);
			o.setCatalogLevel(plevel);

			cmsCatalogMapper.updateByCatalogCodeSelective(o);
			if (cids!=null&&cids.size()>0){
				cmsCatalogMapper.updateCatalogsLevel(cids,i);
			}
			return 1;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return 0;
		}
	}
	public List<Integer> getAllChildOrgaIdList(String parentid) {
		temp = new ArrayList<Integer>();
		initChildCatalogId(parentid);
		return temp;
	}
	private void initChildCatalogId(String parentid){
		List<Integer> list = new ArrayList<Integer>();
		list = cmsCatalogMapper.getChildCatalogIdList(parentid);
		for(Integer element : list){
			temp.add(element);
			if (cmsCatalogMapper.getCountChild(element.toString(),null)>0){
				initChildCatalogId(element.toString());
			}
		}
	}
	@Override
	public CmsCatalog getCatalogByAlias(String alias) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer getAliasCount(String alias) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean updateCheckCode() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<CmsCatalog> getChildByCheckCode(String parentid,
			String catalogtype) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDepartmentTreeByParentId(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmsCatalog findprofessor(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CmsCatalog> findByConditionCatalog() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CmsCatalog findcatalogCheckcode(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CmsCatalog> findByTypeParentId(int parentId, String catalogType) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getcatalogCheckcode(String cid) {
		// TODO Auto-generated method stub
		return null;
	}
}
